package cn.itcast.bos.service.base.impl;

import java.util.Set;
import java.util.UUID;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.dao.base.AreaDao;
import cn.itcast.bos.dao.base.FixeAreaDao;
import cn.itcast.bos.dao.base.OrderDao;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.service.base.OrderService;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
	// 注入Dao层
	@Autowired
	private OrderDao od;
	// 注入区域Dao
	@Autowired
	private AreaDao ad;
	// 注入定区Dao
	@Autowired
	private FixeAreaDao fad;

	// 注入区域的Dao
	@Override
	public void saveOrder(Order order) {
		// 把Area查询出来,再重新赋值,为了让order中的area有Id,可以关联
		UUID uuid = UUID.randomUUID();
		order.setOrderNum(uuid.toString());
		Area sendArea = order.getSendArea();
		Area persistArea = ad.findByProvinceAndCityAndDistrict(
				sendArea.getProvince(), sendArea.getCity(),
				sendArea.getDistrict());
		Area recArea = order.getRecArea();
		Area persistRecArea = ad
				.findByProvinceAndCityAndDistrict(recArea.getProvince(),
						recArea.getCity(), recArea.getDistrict());
		order.setSendArea(persistArea);
		order.setRecArea(persistRecArea);

		System.out.println(order);
		/*
		 * 先根据前台传的寄件人地址匹配客户,如果匹配成功,客户信息上有定区ID, 根据定区ID查找定区中的快递员,然后把订单和快递员进行关联保存
		 */
		String fixedAreaId = WebClient
				.create(Constants.CRM_MANAGEMENT_URL
						+ "/services/customerService/findFixedAreaIdByAddress?address="
						+ order.getSendAddress())
				.type(MediaType.APPLICATION_JSON)
				.accept(MediaType.APPLICATION_JSON).get(String.class);
		// 判断fixedAreaId是否为空
		if (fixedAreaId != null) {
			// 根据ID查找定区
			FixedArea fixedArea = fad.findOne(fixedAreaId);
			// 根据定区查找到响应的快递员
			Courier courier = fixedArea.getCouriers().iterator().next();
			System.out.println(courier);
			order.setCourier(courier);
			od.save(order);
			// 把快递员存入到订单中
			// 保存订单
			return;
		}
		// 如果地址不相等,则根据区域中的模糊查询找到相对应的分区,再根据分区找到定区找到快递员
		// 先根据模糊查询找到相对应的区域
		// 通过区域找到定区
		System.out.println(persistArea);
		Set<SubArea> subareas = persistArea.getSubareas();
		// 遍历定区找到快递员
		for (SubArea subArea : subareas) {
			// 判断用户的下单地址是否在定区中存在
			if (order.getSendAddress().contains(subArea.getKeyWords())
					|| order.getSendAddress().contains(
							subArea.getAssistKeyWords())) {
				Courier courier = subArea.getFixedArea().getCouriers()
						.iterator().next();
				if (courier != null) {
					order.setCourier(courier);
					od.save(order);
					return;
				}
			}
		}
	}

	@Override
	public Order findByOrderNum(String orderNum) {
		return od.findByOrderNum(orderNum);
	}
}
